﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FeCMS.Data
{
    /// <summary>
    ///     自定义的内部数据源，用来向模板传值
    /// </summary>
    public  class TemplateDataSoure
    {
        #region 构造函数

        /// <summary>
        /// 默认就只生成三层的树数据结构
        /// </summary>
        public TemplateDataSoure()
        {
            _levelCount = 3;
            _dataTree = new Dictionary<string, string>[3];
        }

        /// <summary>
        /// 生成N级层级的树数据结构
        /// </summary>
        /// <param name="level">层级</param>
        public TemplateDataSoure(int level)
        {
            _levelCount = level;
            _dataTree = new Dictionary<string, string>[level];
        }

        #endregion


        #region 属性

        
        /// <summary>
        /// 当前所在的层级
        /// </summary>
        public int NowLevel
        {
            set{_nowLevel =value;}
            get{return _nowLevel;}
        }

        /// <summary>
        /// 搜索结果
        /// </summary>
        public string SearchResult
        {
            set;
            get;
        }

        
        /// <summary>
        /// 在同级数据遇到相同的键时是否追加
        /// </summary>
        public bool IsAppend
        {
            set { _isAppend = value; }
        }


        #endregion


        #region 私有变量

        /// <summary>
        /// 树数据
        /// </summary>
        private Dictionary<string, string>[] _dataTree;
        /// <summary>
        /// 层级数
        /// </summary>
        private int _levelCount = 0;
        /// <summary>
        /// 当前所在层级
        /// </summary>
        private int _nowLevel = 0;
        /// <summary>
        /// 在同级数据遇到相同的键时是否追加
        /// </summary>
        private bool _isAppend = false;


        #endregion


        #region 公有方法

        /// <summary>
        /// 根据Key值搜索树数据[默认搜索所有层]
        /// </summary>
        /// <param name="key">健值</param>
        /// <returns></returns>
        public bool SearchLableData(string key)
        {
            return SearchLableData(key,true);
        }

        /// <summary>
        /// 根据Key值搜索树数据
        /// </summary>
        /// <param name="key">健值</param>
        /// <param name="allLevel">是否搜索所有层级</param>
        /// <returns></returns>
        public bool SearchLableData(string key, bool allLevel)
        {
            //搜索所有的层级，直到搜索出
            if (allLevel)
            {
                //层级递减搜索
                for (int nowLevel = _nowLevel; nowLevel >= 0; nowLevel--)
                {
                    if (_dataTree[nowLevel].ContainsKey(key))
                    {
                        SearchResult = _dataTree[nowLevel][key];
                        return true;
                    }
                }
            }
            else //只搜索本层
            {
                if (_dataTree[_nowLevel].ContainsKey(key))
                {
                    SearchResult = _dataTree[_nowLevel][key];
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 根据层级Key值搜索树数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="level">层级</param>
        /// <returns></returns>
        public bool SearchLableData(string key, int level)
        {
            if (level < _levelCount)
            {
                if (_dataTree[level] != null)
                {
                    if (_dataTree[level].ContainsKey(key))
                    {
                        SearchResult = _dataTree[level][key];
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// 添加树数据（本层级）
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        public void AppendLableData(string key,string val)
        {
            if(_dataTree[_nowLevel]==null)
            {
                //防止数据为空
                _dataTree[_nowLevel] =new Dictionary<string,string>();
            }
            //添加数据
            if (!_dataTree[_nowLevel].ContainsKey(key))
            {
                _dataTree[_nowLevel].Add(key, val);
            }
            else
            {
                if (_isAppend)
                {
                    _dataTree[_nowLevel][key] = val;
                }
            }
        }

        /// <summary>
        /// 移除树数据(只移除本层)
        /// </summary>
        /// <param name="key">键</param>
        /// <returns></returns>
        public bool RemoveLableData(string key)
        {
            return RemoveLableData(key,false);
        }

        /// <summary>
        /// 移除树数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="allLevel">是否移除所有层级的。False：只移除本层的 True：移除所有层的</param>
        /// <returns></returns>
        public bool RemoveLableData(string key, bool allLevel)
        {
            bool removeResult = false;
            if (allLevel)
            {
                
                for (int nowLevel = _nowLevel; nowLevel >= 0; nowLevel--)
                {
                    if (_dataTree[nowLevel].ContainsKey(key))
                    {
                        removeResult =true;
                        _dataTree[nowLevel].Remove(key);
                    }
                }
            }
            else
            {
                if (_dataTree[_nowLevel].ContainsKey(key))
                {
                    removeResult = true;
                    _dataTree[_nowLevel].Remove(key);
                }
            }
            return removeResult;
        }

        /// <summary>
        /// 向下一个层级添加树数据
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="val">值</param>
        public void AppendNextLevelLableData(string key, string val)
        {
            //总层级跟目前所在的层级一至
            if (_nowLevel+1 >= _levelCount)
            {
                //扩容
                Dictionary<string, string>[] newBuffer = new Dictionary<string, string>[++_levelCount];
                Array.Copy(_dataTree, newBuffer, _dataTree.Length);
                _dataTree = newBuffer;
            }
            ++_nowLevel;
            AppendLableData(key, val);

        }

        /// <summary>
        /// 向下移动一个层级
        /// </summary>
        /// <returns></returns>
        public int MoveToNextLevel()
        {
            if (_nowLevel + 1 >= _levelCount)
            {
                //扩容
                Dictionary<string, string>[] newBuffer = new Dictionary<string, string>[++_levelCount];
                Array.Copy(_dataTree, newBuffer, _dataTree.Length);
                _dataTree = newBuffer;
            };
            return ++_nowLevel;
        }


        /// <summary>
        /// 向上移动一个层级
        /// </summary>
        /// <returns></returns>
        public int MoveToUpLevel()
        {
            if (_nowLevel - 1 >= 0)
            {
                return --_nowLevel;
            }
            else
            {
                return -1;
            }
        }

        #endregion
    }
}
